package com.konne.nightmare.FastPublicOpinion.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.os.MessageQueue;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.core.view.ViewCompat;

import com.google.gson.Gson;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.bean.LoginBean;
import com.socks.library.KLog;

import java.util.Hashtable;
import java.util.List;
import java.util.Set;

public class Utils {

    public enum FAILED_TYPE {
        CONTENT, SCREENING, DETAIL, DATA_REPORT
    }

    ;

    public static final String TAG = "stcLook";

    public static final int LATERAL_SLIDE_WIDTH = 204;

    public static final String INTENT_TYPE = "intentType";

    public static final String INTENT_DATA = "intentData";

    public static final String INTENT_URL = "intent url";

    public static final String INTENT_TITLE = "intent title";

    public static final String CLIP_BOARD_LABEL = "Label";

    public static final int MY_COLLECTION_CLASS = 0x001;

    public static final int MY_REPORT_CLASS = 0x002;

    public static final int LABEL_DATA_CLASS = 0x003;

    public static final int ZERO_INT = 0x000;

    public static final int ONE_INT = 0x001;

    public static final int TWO_INT = 0x002;

    public static final int THREE_INT = 0x003;

    public static final String BELONGID = "BELONGID";

    public static final String ZERO_STRING = "0";

    public static final String ONE_STRING = "1";

    public static final String TWO_STRING = "2";

    public static final String THREE_STRING = "3";


    public static final String AREA_ID = "areaId";

    public static final String USER_ID = "userId";

    public static final String TOKEN = "token";

    public static final String TITLE = "标题";

    public static final String CONTENT = "正文";

    public static final String SOURCE = "来源";

    public static final String AUTHOR = "作者";

    public static final String APP_DATA = "get login data";

    public static final String DEVICE_ID = "device id";

    public static final String IS_REPORT_PERMISSION = "0";

    public static final String CUSTOMER_ID = "customerId";

    private static View statusBarView;

    /**
     * 设置EditText中hint文字的大小
     *
     * @param hintSize
     * @param hintMessage
     * @return
     */
    public static SpannableString setEditHintSize(int hintSize, String hintMessage) {
        SpannableString hint = new SpannableString(hintMessage);
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(hintSize, true);
        hint.setSpan(absoluteSizeSpan, 0, hint.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return hint;
    }

    public static String formatString(String separator, List<String> param) {
        if (param == null || param.size() == 0) return "";
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : param) {
            if (s == null) continue;
            stringBuilder.append(s).append(separator);
        }
        int len = stringBuilder.length();
        return len != 0 ? stringBuilder.substring(0, len - 1) : stringBuilder.toString();
    }

    /**
     * 侧滑宽度
     */
    public static int slideWidth(Context context, int num) {

        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(outMetrics);
            int width = outMetrics.widthPixels;
            if (num == 2) {
                return width / 2 / 2;
            }
            if (num == 4) {
                return width / 6;
            }
        } else {
            if (num == 2) {
                return 204;
            }
            if (num == 4) {
                return 136;
            }
        }
        return 0;
    }


    /**
     * 隐藏软键盘
     */
    public static void disableKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    public static void initStatusBar(Activity activity) {
        if (statusBarView == null) {
            int identifier = activity.getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = activity.getWindow().findViewById(identifier);
        }
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(R.drawable.title_bluebar);
            statusBarView = null;
        }
    }

    public static boolean isStatusBar() {
        return true;
    }

    public static void bluetoolbar(Activity activity) {
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                if (isStatusBar()) {
                    initStatusBar(activity);
                    activity.getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                            initStatusBar(activity);
                        }

                    });
                }
                //只走一次
                return false;
            }
        });
    }


    /**
     * 获取登录之后的用户信息，贯穿整个APP
     */
    public static void saveAppInfo(LoginBean.DataBean bean) {
        KLog.d(Utils.TAG, Thread.currentThread().getName());
        if (bean == null) return;

        String[] keys = new String[]{
                Utils.TOKEN,
                Utils.APP_DATA,
                Utils.BELONGID};
        MMKVUtils.clearValuesForKeys(keys);
        MMKVUtils.setMMKVInteger(Utils.BELONGID, bean.getBelongId());
        MMKVUtils.setMMKVString(Utils.TOKEN, bean.getToken());
        MMKVUtils.setMMKVString(Utils.APP_DATA, new Gson().toJson(bean));
    }


}
